// pages/todo/add.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		token: '',
		length: 0,
		title: '',
		desc: '',
		showDateTimePicker: false,
		curDate:new Date().getTime(),
		minDate: new Date(2022,10,22).getTime(),
		maxDate: new Date(2200, 1, 1).getTime(),
		selectDateLabel:null,
		// date: "日期",
		// st: "开始",
		// et: "结束",
		location: {
			name: "",
			address: "",
			latitude: "",
			longitude: "",
		},
		types: [{
				no: 0,
				iconPath: "/imgs/activity/png_dance.png",
				title: "跳舞"
			},
			{
				no: 1,
				iconPath: "/imgs/activity/png_eat.png",
				title: "约饭"
			},
			{
				no: 2,
				iconPath: "/imgs/activity/png_beer.png",
				title: "喝酒"
			},
			{
				no: 3,
				iconPath: "/imgs/activity/png_ktv.png",
				title: "唱歌"
			},
			{
				no: 4,
				iconPath: "/imgs/activity/png_note.png",
				title: "其他"
			},
		],
		background: 0,
		selectType: 0,
	},

	//选择活动类型
	selectType(e) {
		console.log(e.currentTarget.dataset.no)
		this.setData({
			selectType: e.currentTarget.dataset.no,
		})
	},


	//统计输入长度
	userInput: function (e) {
		// console.log("输入的内容---" + e.detail.value)
		// console.log("输入的长度---" + e.detail.value.length)
		this.setData({
			length: e.detail.value.length
		})
	},

	// 提交表单
	formSubmit(e) {
		let title = e.detail.value.title
		let desc = e.detail.value.desc
		console.log("title= ", title, ", desc= ", desc, ", token", this.data.token)
		this.setData({
			name: title,
			desc: desc,
		})
		wx.request({
			url: 'https://marklife.love/5h4h/add/activity',
			method: 'POST',
			//传参使用form-data格式
			header: {
				'content-type': 'application/x-www-form-urlencoded' //修改此处即可
			},
			data: {
				token: this.data.token,
				name: this.data.name,
				desc: this.data.desc,
				st: this.data.selectDateLabel,
				et: this.data.selectDateLabel,
				date: this.data.selectDateLabel,
				location_name: this.data.location.name,
				location_address: this.data.location.address,
				location_latitude: this.data.location.latitude,
				location_longitude: this.data.location.longitude,
				background: getApp().getActivityImg(this.data.selectType),
			},
			success: (res) => {
				console.log("add activity: ", res.data)
				var errCode = res.data.err
				if (errCode == 1000) {
					wx.hideLoading()
					//新增成功
					//新增成功提示
					wx.showModal({
						title: '恭喜您',
						icon: "success",
						content: '创建活动成功',
						showCancel: false,
						complete: (res) => {
							if (res.confirm) {
								getApp().globalData.needReRefresh=true;
								wx.navigateBack()
							}
						}
					})
				} else {
					//新增失败 未知原因
					wx.hideLoading()
					wx.showModal({
						title: '新增活动失败',
						content: '抱歉，新增活动失败，我也没时间去排查错误原因：' + errCode,
						showCancel: false,
						complete: (res) => {
							if (res.cancel) {}
							if (res.confirm) {}
						}
					})
				}
			}
		})
	},


	showPopup() {
    this.setData({ showDateTimePicker: true });
  },

  onClose() {
    this.setData({ showDateTimePicker: false });
  },

	selectConfirm(event) {
		const dayjs = require('dayjs')
    this.setData({
			curDate: event.detail,
			showDateTimePicker:false,
			selectDateLabel:dayjs(event.detail).format('YYYY-MM-DD HH:mm'),
		});
	},

	getTimeFormatYYMMDDStr(time) {
		const dayjs = require('dayjs')
    return dayjs(time).format('YYYY-MM-DD')
	},

	getTimeFormatYYMMDDHHMMStr(time) {
		const dayjs = require('dayjs')
    return dayjs(time).format('YYYY-MM-DD HH:mm')
	},

	selectCancel(event) {
    this.setData({
			showDateTimePicker:false,

		});
	},



	//选择了日期
	pickDate(e) {
		console.log(e.detail.value)
		this.setData({
			date: e.detail.value,
		})
	},

	//选择了开始时间
	pickSt(e) {
		console.log(e.detail.value)
		this.setData({
			st: e.detail.value,
		})
	},

	//选择了结束时间
	pickEt(e) {
		console.log(e.detail.value)
		this.setData({
			et: e.detail.value,
		})
	},

	//选择地点
	chooseLocation() {
		var chooseLatitude = 31.181713
		var chooseLongitude = 121.506117
		var chooseAddress = "上海市浦东新区云莲路201号三林体育中心2楼"
		var chooseName = "WIN OF DANCE STUDIO(三林旗舰店)"
		if (this.data.location.latitude.length > 0 || this.data.location.longitude.length > 0) {
			chooseLatitude = this.data.location.latitude
			chooseLongitude = this.data.location.longitude
			chooseAddress = this.data.location.address
			chooseName = this.data.location.name
		}
		wx.chooseLocation({
			latitude: chooseLatitude,
			longitude: chooseLongitude,
			name: chooseName,
			address: chooseAddress,
			success: (loc) => {
				console.log(loc)
				this.setData({
					location: {
						name: loc.name,
						address: loc.address,
						latitude: loc.latitude,
						longitude: loc.longitude,
					}
				})
			}
		})
	},

	chooseImage() {
		wx.chooseMedia({
			count: 1,
			mediaType: ['image'],
			camera: 'back',
			success: (res) => {
				console.log("chooseImage " + res.tempFiles[0].tempFilePath)
				this.setData({
					background: res.tempFiles[0].tempFilePath
				})
			}
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		const paramsToken = options.token;
		this.setData({
			token: paramsToken,
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})